layui.define(['layer', 'form','jquery','element','layedit'], function(exports){
	"use strict";
    var $ = layui.jquery,
      element = layui.element()
	  ,layer = layui.layer
	  ,layedit = layui.layedit
	  ,form = layui.form();
	
	 var $windowParentDoc = window.parent.document,
	  	$tabTitle=$('.layui-tab-title',$windowParentDoc),
	  	$tabContent=$('.layui-tab-content',$windowParentDoc),
	    $currIndex=$($('.layui-tab-title li.layui-this',$windowParentDoc)).index()+1;
	 
	 var layeditIndex =layedit.build('content_one'); //建立编辑器
	
	 var $allCho=$('input[lay-filter="departAllChose"]')
	 form.on('checkbox(departChose)', function(data){
		 var inpLen=$allCho.siblings('input:not(input[type="hidden"])').length;
		 var chkedLen=$allCho.siblings('input:not(input[type="hidden"]):checked').length;
		 console.log(chkedLen);
		if(chkedLen==inpLen){
			$allCho.prop("checked",true);
		}else if(chkedLen<inpLen){
			$allCho.prop("checked",false);
		}
		form.render('checkbox');
	});
	 
	//全选
   form.on('checkbox(departAllChose)', function(data){
		var nextSibl = $(this).siblings('input:not(input[type="hidden"])');
	    nextSibl.each(function(index, item){
			item.checked = data.elem.checked;
		});	
		form.render('checkbox');
		
	});
   form.on('radio(factstatus)', function(data){
		if(data.value==1){
			$("#remark").hide();
		}else{
			$("#remark").show();
		}
		form.render('radio');
	}); 
   
   form.on('checkbox(levelChose)', function(data){
	   var inpLen=$allCho.siblings('input:not(input[type="hidden"])').length;
	   var chkedLen=$allCho.siblings('input:not(input[type="hidden"]):checked').length;
	   console.log(chkedLen);
	   if(chkedLen==inpLen){
		   $allCho.prop("checked",true);
	   }else if(chkedLen<inpLen){
		   $allCho.prop("checked",false);
	   }
	   form.render('checkbox');
   });
   
   //全选
   form.on('checkbox(levelAllChose)', function(data){
	   var nextSibl = $(this).siblings('input:not(input[type="hidden"])');
	   nextSibl.each(function(index, item){
		   item.checked = data.elem.checked;
	   });	
	   form.render('checkbox');
	   
   });
	   
	/* 验证 */ 
	form.verify({
		uname:function(v){
			
			if(v.length<1){
				return "请输入用户名";
			}else if(v.length>50||v.length<2){
				return "用户名2-50字之间";
			}
		},
		reluname:function(v){
			if(v.length<1){
				return "请输入真实姓名";
			}else if(v.length>50||v.length<2){
				return "用户名2-50字之间";
			}
		},
		sfz:function(v){
			if(!/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/g.test(v)){
				return "请输入身份证";
			}
		},
		yxq:function(v){
			if(!v){
				return "请选择有效期";
			}
		},
		pphone:function(v){
			if(!/^1\d{10}$/g.test(v)){
				return "请输入手机号";
			}
			
		},
		department:function(v){
			if(v.length<1){
				return "请选择所属部门";
			}
		},
		role:function(v){
			if(v.length<1){
				return "请选择所属角色";
			}
		},
		sex:function(v){
			if($('input[name="sex"]:checked').length==0){
				return "请选择性别";
			}
		},
		question_content: function (v) {
			var value = layedit.getContent(layeditIndex);
			if (!value) {
				return '内容不能为空'
			}
		},
		email:function(v){
			if(!/\w+[@]{1}\w+[.]\w+/g.test(v)){
				return "请输入正确的邮箱号";
			}
		},
		onlynum:function(v){
			if(!/^\d{n}$/g.test(v)){
				return "请输入n位数字";
			}
		}
		
	 });
	
	 /* 提交 */
	 form.on('submit(commonSubmit)', function(data){ 
		 var departCkdsVal=[];
		 var $allCho=$('input[lay-filter="departAllChose"]')
		 $allCho.siblings('input:not(input[type="hidden"]):checked').each(function(){
			 departCkdsVal.push($(this).val());
		 });
		 departCkdsVal=departCkdsVal.join(',');
		 $('input[name="sydepart"]').val(departCkdsVal);
		 
		 var levelCkdsVal=[];
		 var $allCho=$('input[lay-filter="levelAllChose"]')
		 $allCho.siblings('input:not(input[type="hidden"]):checked').each(function(){
			 levelCkdsVal.push($(this).val());
		 });
		 levelCkdsVal=levelCkdsVal.join(',');
		 $('input[name="level"]').val(levelCkdsVal);
		 
		 layedit.sync(layeditIndex);
		 
		 $('select option').eq(1).attr('selected',true);
		 console.log($('select[name="department"]').val());
	     $.ajax({
	    	 type: 'POST',
			  url: $('#commonForm').attr("action"),
			  data: $('#commonForm').serialize(),
			  success:function(data){
				  console.log(data)
				  if(data.code==200){
					  var onlyrel=data.url;
					 if(onlyrel){
						 if(parent.layer.getFrameIndex(window.name)) {
							  parent.location.reload(true);
							  parent.layer.close(parent.layer.getFrameIndex(window.name));
							  parent.layer.msg(data.msg, {time: 3000, icon:3});
						 } else {
							  parent.layer.msg(data.msg, {time: 3000, icon:6})
							  initManage(onlyrel);		
						 }
					  }else{
						  window.location.reload(true);
					  }
				  }else{
					  layer.msg(data.msg, {time: 300, icon:3});
					  return;
				  }
			  },
			  error:function(){
				  layer.msg("请求失败", {time: 300});
			  }
	    });
	    
	    return false;
	  });
 
      // 取消按钮操作
	 
	 $(".xbzx_btn_save_group").find("a[xbzx-data-type='cancel']").on("click",function() {
		 if(parent.layer.getFrameIndex(window.name)) {
			  var msg = '正在取消...'
			  parent.location.reload(true);
			  parent.layer.close(parent.layer.getFrameIndex(window.name));
			  parent.layer.msg(msg, {time: 1500, icon:3});
		 } else {
			 var msg = '正在取消...'
			   parent.layer.msg(msg, {time: 1500, icon:6})
			  var currId=$('.layui-tab-title li:nth-child('+$currIndex+')',$windowParentDoc).attr('lay-id');  
			  parent.layui.element().tabDelete('xbzx-cms-tab', currId);
		 }
	 })
		  	
	  function initManage(onlyrel){
		  $('iframe[id="iNow"]',$windowParentDoc).each(function(i){
			  if($(this).attr('data-rel') === onlyrel){
				  var ifName = $(this).attr('name');
				  window.parent.frames[ifName].location.reload(true);
			  }
		  })
//		  parent.document.getElementById(onlyrel).contentWindow.location.reload(true);

		//切换到指定页面
		  parent.layui.element().tabChange('xbzx-cms-tab', onlyrel);
		  
		  //关闭当前
		  var currId=$('.layui-tab-title li:nth-child('+$currIndex+')',$windowParentDoc).attr('lay-id');  
		  parent.layui.element().tabDelete('xbzx-cms-tab', currId);
		  
		  return false;
	  }
	  exports('commonsubmit',{});
});
